package com.ophiux.micro.core.domain.page;

import java.util.HashMap;
import java.util.Map;

import com.ophiux.micro.core.domain.ValueObject;

/**
 * 说明：Domain层  -- 分页配置对象<br>
 * <h1>创 建 人: hehailong<br></h1>
 * 创建日期: 2018年8月23日 上午10:49:12<br>
 * 修改记录：
 */
public class PageConfig extends ValueObject {
	
	private int pageIndex;	//页标，从1开始
	
	private int pageSize;	//页大小
	
	private Map queryPredicate;	//查询谓词,即条件

	public PageConfig(int pageIndex, int pageSize, Map queryPredicate) {
		super();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.queryPredicate = queryPredicate;
	}

	public PageConfig(int pageIndex, int pageSize) {
		super();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public Map getQueryPredicate() {
		return queryPredicate;
	}

	public void setQueryPredicate(Map queryPredicate) {
		this.queryPredicate = queryPredicate;
	}
	
	/**
	 * 全部内容转为map
	 * @return
	 */
	public Map all2Map() {
		
		Map allMap = new HashMap<String , String>();
		allMap.putAll(this.queryPredicate);
		
		allMap.put("start", this.caclAndGetStartRow());
		allMap.put("length", this.pageSize);
		
		return allMap;
	}
	
	/**
	 * 计算并返回开始行
	 * @return
	 */
	public int caclAndGetStartRow() {
		return (this.pageIndex - 1) * this.pageSize;
	}
	
	/**
	 * 添加查询条件-谓词
	 * @param column	数据库表的列，在此不区分表，自己再sql中区分
	 * @param value	值   暂时全部弄成 字符串
	 */
	public void addPredicate(String column , String value) {
		if(this.queryPredicate == null) {
			this.queryPredicate = new HashMap<String , String>();
		}
		this.queryPredicate.put(column, value);
	}
	
	public void addPredicateMap(Map map) {
		if(map == null) return;
		if(this.queryPredicate == null) {
			this.queryPredicate = new HashMap<String , String>();
		}
		this.queryPredicate.putAll(map);
	}
	
}
